package org.csu.mypetstorebackstage.service;

import org.csu.mypetstorebackstage.domain.LineItem;
import org.csu.mypetstorebackstage.domain.OrderItem;
import org.csu.mypetstorebackstage.domain.OrderSales;
import org.csu.mypetstorebackstage.persistence.LineItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LineItemService {
    @Autowired
    LineItemMapper lineItemMapper;


    public List<OrderItem> getOrderItem(){
        List<OrderItem> orderItemList=new ArrayList<OrderItem>();
        List<LineItem> lineItemList=lineItemMapper.getAllLineItem();
                OrderItem orderItem=new OrderItem();
        orderItem.setItemId(lineItemList.get(0).getItemId());
        orderItem.setOrderId(lineItemList.get(0).getOrderId());
        orderItem.setUnitPrice(lineItemList.get(0).getUnitPrice());
        orderItemList.add(orderItem);
        for(int i=1;i<lineItemList.size();i++){
            String currentitemId=lineItemList.get(i-1).getItemId();

            if(!(lineItemList.get(i).getItemId().equals(currentitemId))){
                OrderItem orderItem1=new OrderItem();
                orderItem1.setItemId(lineItemList.get(i).getItemId());
                orderItem1.setOrderId(lineItemList.get(i).getOrderId());

                orderItem1.setUnitPrice(lineItemList.get(i).getUnitPrice());
                orderItemList.add(orderItem);
            }
            else{
                for(int k=0;k<orderItemList.size();k++){
                    if(lineItemList.get(i).getItemId().equals(orderItemList.get(k).getItemId())){

                        BigDecimal temp=orderItemList.get(k).getUnitPrice();
                        orderItemList.get(k).setUnitPrice(lineItemList.get(i).getUnitPrice().add(temp));
                    }
                }

            }
        }
        return  orderItemList;

    }
}
